$(function () {
    var paylistbill_datagrid = $("#paylistbill_datagrid");
    var paylistbill_dialog = $("#paylistbill_dialog");
    var pay_dialog = $("#pay_dialog");
    var paylistbill_form = $("#paylistbill_form");
    var pay_form = $("#pay_form");

    paylistbill_datagrid.datagrid({
        url: '/accident/list1',
        fit: true,
        toolbar: '#tb',
        fitColumns: true,
        rownumbers: true,
        sortName: 'hireDate',
        sortOrder: 'asc',
        singleSelect: true,
        pagination: true,
        columns: [[
            {field: 'id', checkbox: true},
            {
                field: 'status', title: '当前状态', width: 80, formatter: function (value, row, index) {
                    if(value==0){
                        return "<font color='#a52a2a'>待验证</font>";
                    }
                    else if (value == 1) {
                        return "<font color='#00008b'>验证完,待填理赔</font>";
                    } else if (value == 2) {
                        return "<font color='#00ced1'>废弃</font>";
                    } else if (value == 3) {
                        return "<font color='#6495ed'>待审核</font>";
                    } else if (value == 4) {
                        return "<font color='#daa520'>被拒审核</font>";
                    } else if (value == 5) {
                        return "<font color='#ff6347'>审核完,待付款给保人</font>";
                    } else if (value == 6) {
                        return "<font color='red'>已赔付</font>";
                    }
                    return false;
                }
            },
            {
                field: "client", title: "报案人姓名", width: 100,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },
            {
                field: "guaranteeBill", title: "保险单号", width: 100, formatter: function (value, row, index) {
                    if(value==null){
                        return;
                    }
                    return value.number;
                }
            },
            {
                field: "tel", title: "联系方式", width: 100, formatter: function (value, row, index) {
                    if (row.client) {
                        return row.client.tel;
                    } else {
                        return "";
                    }
                }
            },
            {field: 'oTime', title: '报案时间', width: 80},
            {field: 'duty', title: '事故责任', width: 80},
            {
                field: "inputUser", title: "录入人", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "";
                    }
                }
            },
            {field: 'remark', title: '备注', width: 80}
        ]],
       
    });

    paylistbill_dialog.dialog({
        width: 400,
        height: 500,
        closed: true,
        modal: true,
        buttons: '#bb'
    });

    pay_dialog.dialog({
        width: 400,
        height: 300,
        closed: true,
        modal: true,
       buttons: '#cc'
    });


    //所用的点击事件，都通过这个方法来调用对应的方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObject[cmd]();
    });

    //把所有的方法放到一个对象中，方便管理
    var methodObject = {
        /*查看详情*/
        look: function () {
            var row = paylistbill_datagrid.datagrid('getSelected');
            console.log(row);
            //如果没有选择员工的一条记录
            if (row == null) {
                $.messager.alert('温馨提示', '至少选择一条记录', 'error');
                return;
            }
            /*row.cname = row.paymentResult.applicant.name;*/
            if (row.client) {
                row['client.name'] = row.client.name;
            }
            paylistbill_form.form('clear');
            paylistbill_form.form('load', row);//加载记录来填充表单
            paylistbill_dialog.dialog('open');
            paylistbill_dialog.dialog('setTitle', '查看详情');

        },
        /*添加应赔金额*/
        edit: function () {
            var row = paylistbill_datagrid.datagrid('getSelected');
            //row.id;
            //如果没有选择员工的一条记录
            if (row == null) {
                $.messager.alert('温馨提示', '至少选择一条记录', 'error');
                return;
            }
            if (row.guaranteeBill) {
                row["guaranteeBill.number"] = row.guaranteeBill.number;
            }


            pay_form.form('clear');
            pay_form.form('load', row);//加载记录来填充表单
            pay_dialog.dialog('open');
            pay_dialog.dialog('setTitle', '编辑赔偿明细');

        },

        /*提审*/
        check: function () {
            var row = paylistbill_datagrid.datagrid('getSelected');
            //如果没有选择员工的一条记录
            if (row == null) {
                $.messager.alert('温馨提示', '至少选择一条记录', 'error');
                return;
            }
                    //1.发起请求修改状态
                    //先查询是否有赔损单数据 没有不允许提交审核 通过 cid 来查
                    $.get('/accident/hasCom', { id: row.id}, function (data) {
                        if (data.success) {

                            $.messager.confirm('确认', '您确定要执行这个操作?', function (r) {
                                if (r) {
                                    $.get('/accident/changeStatus1', {id: row.id}, function (data1) {
                                        if (data1.success) {
                                            $.messager.alert('温馨提示', '更改成功', 'info', function () {
                                                paylistbill_datagrid.datagrid('reload');
                                            });
                                        } else {
                                            $.messager.alert('温馨提示', '更改失败', 'error', function () {
                                                paylistbill_datagrid.datagrid('reload');
                                            });
                                        }
                                    });
                                }
                            })


                        } else {
                            $.messager.alert('温馨提示', '请先填写赔损单', 'error', function () {
                                paylistbill_datagrid.datagrid('reload');
                            });
                        }
                    })

        },


          /*  //删除赔损单
            $.get('/compensationInfo/deleteByPrimaryKey.do', {id: row.id}, function (data) {

            })
        },*/

        /*废弃*/
        abandon: function () {
            var row = paylistbill_datagrid.datagrid('getSelected');
            //如果没有选择员工的一条记录
            if (row == null) {
                $.messager.alert('温馨提示', '至少选择一条记录', 'error');
                return;
            }

            $.messager.confirm('确认', '您确定要执行这个操作?', function (r) {
                if (r) {
                    //1.发起请求修改状态
                    //先查询是否有赔损单数据 没有不允许提交审核 通过 cid 来查
                    $.get('/accident/changeStatus4', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '更改成功', 'info', function () {
                                paylistbill_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', '更改失败', 'error', function () {
                                paylistbill_datagrid.datagrid('reload');
                            });
                        }
                    })
                }
            })


            /*  //删除赔损单
              $.get('/compensationInfo/deleteByPrimaryKey.do', {id: row.id}, function (data) {

              })*/
        },
        /*保存赔偿明细单*/
        save: function () {
            var id = $("#payId").val();

            console.log(id);
            var row = paylistbill_datagrid.datagrid('getSelected');
            console.log(row);
            /*var gId = row.guaranteeBill.id;
            var aId = row.id;*/


            pay_form.form('submit', {
                url: '/compensationInfo/saveOrUpdate' ,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                           pay_dialog.dialog('close');
                            paylistbill_datagrid.datagrid('reload');
                        })
                    } else {
                        $.messager.alert('温馨提示', '保存失败', 'error', function (r) {
                            if (r) {
                                pay_dialog.dialog('close');
                                paylistbill_datagrid.datagrid('reload');
                            }
                        })
                    }
                },

                /*把保单一起保存*/
                onSubmit: function (param) {
                    var row = paylistbill_datagrid.datagrid('getSelected');
                    if (row == null) {
                        return;
                    }
                    var gId = row.guaranteeBill.id;
                    var aId = row.id;
                    $("#gId").val(gId);
                    $("#aId").val(aId);
                 /*   param.client_id = row.guaranteeBill.policyClient.id;*/

                }
            })
        },

        /*关闭对话框*/
        cancel1: function () {
            pay_dialog.dialog('close');
        },
        cancel: function () {
            paylistbill_dialog.dialog('close');
        },


        /*刷新*/
        reload: function() {
            paylistbill_datagrid.datagrid("reload");
        }
    }

});

